﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        Console.Write("请输入C#源文件的路径：");
        string filePath = Console.ReadLine();

        if (File.Exists(filePath))
        {
            Dictionary<string, int> wordCounts = new Dictionary<string, int>();

            string[] lines = File.ReadAllLines(filePath);

            foreach (string line in lines)
            {
                // 删除空行和注释（以"//"开头）
                string cleanedLine = line.Trim();
                if (!string.IsNullOrWhiteSpace(cleanedLine) && !cleanedLine.StartsWith("//"))
                {
                    // 统计单词数（不包括空格、标点符号）
                    string[] words = Regex.Split(cleanedLine, @"\P{L}+");
                    foreach (string word in words)
                    {
                        string cleanedWord = word.Trim();
                        if (!string.IsNullOrWhiteSpace(cleanedWord))
                        {
                            if (wordCounts.ContainsKey(cleanedWord))
                            {
                                wordCounts[cleanedWord]++;
                            }
                            else
                            {
                                wordCounts[cleanedWord] = 1;
                            }
                        }
                    }
                }
            }

            Console.WriteLine("单词\t\t出现次数");
            Console.WriteLine("------------------------");
            foreach (var kvp in wordCounts)
            {
                Console.WriteLine($"{kvp.Key}\t\t{kvp.Value}");
            }
        }
        else
        {
            Console.WriteLine("文件不存在！");
        }

        Console.ReadLine();
    }
}